//
//  YSDirectoryViewController.m
//  YSBookRead
//
//  Created by MOLBASE on 2018/11/15.
//  Copyright © 2018年 YangShen. All rights reserved.
//

#import "YSDirectoryViewController.h"
#import "YSDirectoryCell.h"
#import "YSBookChapterContentModel.h"

@interface YSDirectoryViewController () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UIButton *goButton;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UIView *moveView;
@property (weak, nonatomic) IBOutlet UIImageView *moveImageView;

@property (assign, nonatomic) BOOL firstDisplay;
@property (assign, nonatomic) CGFloat beginOffsetY;

@end

@implementation YSDirectoryViewController

- (void)viewWillAppear:(BOOL)animated {
    [super viewWillAppear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:NO withAnimation:UIStatusBarAnimationFade];
    _firstDisplay = YES;
    [self.tableView reloadData];
}

- (void)viewWillDisappear:(BOOL)animated {
    [super viewWillDisappear:animated];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationFade];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.moveView.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.85];
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([YSDirectoryCell class]) bundle:nil] forCellReuseIdentifier:NSStringFromClass([YSDirectoryCell class])];
    self.tableView.estimatedRowHeight = 0;
    self.tableView.estimatedSectionFooterHeight = 0;
    self.tableView.estimatedSectionHeaderHeight = 0;
    self.tableView.rowHeight = 44;
}

- (IBAction)closeButtonDidClick:(id)sender {
    [self dismissViewControllerAnimated:YES completion:nil];
}

- (IBAction)goToAction:(id)sender {
    if ([self.goButton.currentTitle isEqualToString:@"到顶部"]) {
        self.moveImageView.image = [UIImage imageNamed:@"move-up"];
        [UIView animateWithDuration:0.05 animations:^{
            self.moveView.alpha = 1.0;
        }];
        [self.tableView setContentOffset:CGPointMake(0, 0) animated:YES];
    } else {
        self.moveImageView.image = [UIImage imageNamed:@"move-down"];
        [UIView animateWithDuration:0.05 animations:^{
            self.moveView.alpha = 1.0;
        }];
        [_tableView setContentOffset:CGPointMake(0, (_chaptersArr.count) * _tableView.rowHeight - _tableView.bounds.size.height) animated:YES];
    }
}

#pragma mark - UITableViewDelegate, UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.chaptersArr.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    YSDirectoryCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([YSDirectoryCell class]) forIndexPath:indexPath];
    NSUInteger count = _chaptersArr.count - indexPath.row - 1;
    cell.isReadingChapter = count == self.readingChapter;
    cell.count = count + 1;
    cell.chapterModel = self.chaptersArr[count];
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    return cell;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.row < self.chaptersArr.count) {
        BOOL needUpdate = self.readingChapter != (_chaptersArr.count - indexPath.row - 1);
        [self dismissViewControllerAnimated:YES completion:^{
            if (needUpdate && self.selectChapter) {
                self.selectChapter(self.chaptersArr.count - indexPath.row - 1);
            }
        }];
    }
}

- (void)tableView:(UITableView *)tableView willDisplayCell:(nonnull UITableViewCell *)cell forRowAtIndexPath:(nonnull NSIndexPath *)indexPath {
    if (_firstDisplay) {
        _firstDisplay = NO;
        CGFloat offsetY = (_chaptersArr.count - 1 - _readingChapter) * tableView.rowHeight - tableView.bounds.size.height/2;
        if (offsetY > _chaptersArr.count * tableView.rowHeight - tableView.bounds.size.height) {
            offsetY = _chaptersArr.count * tableView.rowHeight - tableView.bounds.size.height;
        } else if (offsetY < 0) {
            offsetY = 0;
        }
        tableView.contentOffset = CGPointMake(0,offsetY);
    }
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView {
    self.beginOffsetY = scrollView.contentOffset.y;
}

- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView {
    [self showGoToBtn];
    if (scrollView.contentOffset.y > self.beginOffsetY) {
        [self.goButton setTitle:@"到底部" forState:UIControlStateNormal];
        self.goButton.tag = 100;
    } else {
        [self.goButton setTitle:@"到顶部" forState:UIControlStateNormal];
        self.goButton.tag = 101;
    }
}

- (void)scrollViewDidEndScrollingAnimation:(UIScrollView *)scrollView {
    [self hideGoToBtn];
}

- (void)showGoToBtn {
    if (self.goButton.alpha >= 0.9) {
        return;
    }
    [UIView animateWithDuration:0.1 animations:^{
        self.goButton.alpha = 1;
    }];
}

- (void)hideGoToBtn {
    if (self.goButton.alpha < 0.1) {
        return;
    }
    [UIView animateWithDuration:0.1 animations:^{
        self.goButton.alpha = 0;
        self.moveView.alpha = 0.0;
    }];
}

@end
